{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "945e27b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install laspy[lazrs,laszip] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74dc807e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "from k3d.helpers import download\n",
    "import numpy as np\n",
    "import laspy \n",
    "import pandas as pd\n",
    "\n",
    "files = ['https://opendata.geoportal.gov.pl/NumDaneWys/DanePomiaroweLAZ/4979/4979_279934_M-34-101-A-c-4-3-1.laz',\n",
    "         'https://opendata.geoportal.gov.pl/NumDaneWys/DanePomiaroweLAZ/4979/4979_279935_M-34-101-A-c-4-3-2.laz',\n",
    "         'https://opendata.geoportal.gov.pl/NumDaneWys/DanePomiaroweLAZ/4979/4979_279933_M-34-101-A-c-4-1-4.laz',\n",
    "         'https://opendata.geoportal.gov.pl/NumDaneWys/DanePomiaroweLAZ/4979/4979_279932_M-34-101-A-c-4-1-3.laz',\n",
    "         'https://opendata.geoportal.gov.pl/NumDaneWys/DanePomiaroweLAZ/4979/4979_279929_M-34-101-A-c-3-2-4.laz']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d06a574-da53-493d-b8fa-9dbc7a7faffc",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "for url in files:\n",
    "    file = download(url)\n",
    "    d = laspy.read(file)    \n",
    "    data.append(pd.DataFrame(d.points.array))\n",
    "\n",
    "data = pd.concat(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2734b5b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7cec11ca-fd2b-44ba-a4dd-2dde208aa65e",
   "metadata": {},
   "outputs": [],
   "source": [
    "xyz = data[['X', 'Y', 'Z']].to_numpy().astype(np.float32) / 1000000\n",
    "rgb = data[['red', 'green', 'blue']].to_numpy().astype(np.float32) / 50000\n",
    "rgb = np.sum((np.clip(rgb, 0, 1) * 255.0).astype(np.uint32) * np.array([1, 256, 256 ** 2]), axis=1).astype(np.uint32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e05b85c-db7a-4f62-964a-633ec1233e14",
   "metadata": {},
   "outputs": [],
   "source": [
    "xyz.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afea13bb-2463-4604-8867-a9422879701e",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot = k3d.plot()\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9f7ea83-ae67-430c-927d-227f07eaef03",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot += k3d.points(xyz[::5, :], rgb[::5], shader='3d', point_size=0.0005)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bdf8c55-efbf-4d87-bfb1-fcc414420fa0",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = [57.819305635444245,\n",
    " 14.889169188689477,\n",
    " 0.21094970995023637,\n",
    " 57.79127352356057,\n",
    " 14.727631627415766,\n",
    " 0.1694874872923474,\n",
    " -0.05936908553010498,\n",
    " -0.183565449015819,\n",
    " 0.9812130439465935]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5b947a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
